(function(window){
	
	

	PlanetsAnimation.NORTH = -1;
	PlanetsAnimation.SOUTH = 1;
	PlanetsAnimation.POLE = PlanetsAnimation.SOUTH;
	PlanetsAnimation.prototype.interval;
	PlanetsAnimation.d = new Date();
	PlanetsAnimation.sunSize = 40;
	PlanetsAnimation.moonSize = 7;
	PlanetsAnimation.bgSize = 720;
	PlanetsAnimation.bgOrbitSize = 720;
	PlanetsAnimation.monthNames = [ null, "January", "February", "March", "April", "May",
	      			"June", "July", "August", "September", "October", "November",
	      			"December" ]
	PlanetsAnimation.dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
	PlanetsAnimation.moonOrbitRadius = 20;
	PlanetsAnimation.orbitGap = (PlanetsAnimation.bgOrbitSize>>1)/9;
	PlanetsAnimation.planets = new Array();
	
	PlanetsAnimation.cos = NumberUtils.cosD;
	PlanetsAnimation.sin = NumberUtils.sinD;


	PlanetsAnimation.centre = {
		x : 500,
		y : 450
	};
	
	PlanetsAnimation.prototype.rotationOffset = 0;
	PlanetsAnimation.prototype.date = new Date();
	PlanetsAnimation.prototype.earthPos = {};


	function PlanetsAnimation() {
		this.dateOutElement = document.getElementById("dateOut")
		var o = document.getElementById("earth");
		PlanetsAnimation.moon = document.getElementById("moon");
	};
	PlanetsAnimation.prototype.getDateWithSuffix = function(date){
		var suffix
		var dateString = date;
		dateString = dateString<10 ? "0"+dateString : dateString.toString();
		var switchDate
		if(dateString.length == 2){
			switchDate = dateString.substr(0, 1) == "1" ? 0 : dateString.substr(1, dateString.length-1);
			dateString = dateString.substr(0, 1) == 0 ? dateString.substr(1, 1) : dateString;
		} else {
			switchDate = dateString;
		}
		switch(switchDate){
			case "1":
			suffix = date+"st";
			break;
			case "2":
			suffix = date+"nd";
			break;
			case "3":
			suffix = date+"rd";
			break;
			default :
			suffix = date+"th";
			break;
		}
		return suffix;
		
	}
	PlanetsAnimation.prototype.dateOut = function(date){
		var hours = date.getHours() < 10 ? date.getHours() == 0 ? "00" : "0"+date.getHours() : date.getHours();
 		var minutes = date.getMinutes() < 10 ? date.getMinutes() == 0 ? "00" : "0"+date.getMinutes() : date.getMinutes();
		var dateOutString = PlanetsAnimation.dayNames[date.getDay()]+", "+PlanetsAnimation.monthNames[date.getMonth() + 1] + ", "+date.getDate()+", " + date.getFullYear().toString()+", "+hours+":"+minutes;
		return dateOutString;
	}
	PlanetsAnimation.getTopLeft = function(azimuth, altitude){
		var top = ((((NumberUtils.cosD(azimuth) * (((90-altitude)/180)*PlanetsAnimation.bgSize)* PlanetsAnimation.POLE))))>>0;
		var left = ((NumberUtils.sinD(azimuth) * (((90-altitude)/180)*PlanetsAnimation.bgSize) * PlanetsAnimation.POLE)) >> 0;	
		return {top : top, left : left};
	}
	PlanetsAnimation.prototype.azimuthAltitude = function(date, elements){
		this.date = date;
		this.dateOutElement.innerHTML = this.dateOut(date);
		for(var i = 0; i<elements.length; ++i){
			if(elements[i].name != "bg" && elements[i].name != "earth"){
				var pData = PlanetaryPositions[elements[i].name](date);
				var topLeft = PlanetsAnimation.getTopLeft(pData.azimuth, pData.altitude)
				elements[i].x = (topLeft.left);
				elements[i].y = (topLeft.top);
				elements[i].RA = pData.RA;
				elements[i].Dec = pData.Dec;
				elements[i].azimuth = pData.azimuth;
				elements[i].altitude =pData.altitude;
				if(elements[i].name != "moon"){
					elements[i].z = (1000-((pData.r*10)>>0));
				} else {
					elements[i].phase = pData.moonPhase
					elements[i].z = (2000);
				}
			}
		}
	}
	PlanetsAnimation.prototype.circularOrbit = function(date) {
		
		var sun = SolarSystem.getElement("sun");
		sun.x = 0;
		sun.y = 0;
		
		this.date = date
		this.dateOutElement.innerHTML = this.dateOut(date);
		var count = 0;
		 for(var i = 0; i<SolarSystem.elements.length; ++i){

		 	if(SolarSystem.elements[i].name != "bg" && SolarSystem.elements[i].name != "sun" && SolarSystem.elements[i].name != "moon"){
				var pData = PlanetaryPositions[SolarSystem.elements[i].name](date);
				var r = ((count + 1) * PlanetsAnimation.orbitGap);
				var angle = pData.lonecl + this.rotationOffset;
				if (SolarSystem.elements[i].name == "earth") {
					this.earthPos.x = (NumberUtils.cosD(angle) * r);
					this.earthPos.y = ((NumberUtils.sinD(angle) * r) * PlanetsAnimation.POLE);
					SolarSystem.elements[i].x = ((this.earthPos.x)>>0);
					SolarSystem.elements[i].y = ((this.earthPos.y)>>0);
				} else {
					SolarSystem.elements[i].x = (((NumberUtils.cosD(angle) * r)) >> 0);
					SolarSystem.elements[i].y = (((((NumberUtils.sinD(angle) * r) * PlanetsAnimation.POLE)))>>0);
					
				}
				++count;
			}
		 }
		var moonAngle = PlanetaryPositions.moon(date).lonecl+ this.rotationOffset;
		SolarSystem.getElement("moon").x = (((this.earthPos.x)+ (NumberUtils.cosD(moonAngle) * PlanetsAnimation.moonOrbitRadius))>>0);
		SolarSystem.getElement("moon").y = (((this.earthPos.y)+ ((NumberUtils.sinD(moonAngle) * PlanetsAnimation.moonOrbitRadius) * PlanetsAnimation.POLE))>>0);
	}
	PlanetsAnimation.prototype.init = function(){
		var bg = SolarSystem.getElement("bg");
		bg.x = 0;
		bg.y = 0;
		this.date = new Date();
	}
	if(!window.PlanetsAnimation) window.PlanetsAnimation = PlanetsAnimation;
}(window))